<?php
// +----------------------------------------------------------------------
// | 黄金十月活动主题
// +----------------------------------------------------------------------

namespace Toppic\Controller;
use Common\Controller\Base;
use Common\Model\Model;

class OctoberController extends Base {

    public function index(){
        //获取浏览量最大的七个作品
        $oWorks = $octoberOnt = M('Works') 
                 ->alias('W')
                 ->join('__USER__ U ON U.userid = W.userid')
                 ->where(array('W.type'=>1))
                 ->limit(7)
                 ->order('W.clicknum DESC')
                 ->select();

        //获取活动作品
        $number = M('Works')->where(array('type'=>1,'activity'=>1))->count();
        $number = $number + 100;

        $works = $octoberOnt = M('Works') 
                 ->alias('W')
                 ->join('__USER__ U ON U.userid = W.userid')
                 ->where(array('W.type'=>1,'W.activity'=>1))
                 ->order('W.goodnum DESC')
                 ->select();


    	//活动的评论
    	$limitRows = 10; // 设置每页记录数
    	$count = M('October')->where(array('octoberid'=>0))->count();
    	$page = $this->ajaxpage($count,$limitRows,'type=msg','ajax_div','ajax_page_div');
    	$octoberOnt = M('October') 
    	             ->alias('O')
    	             ->join('__USER__ U ON U.userid = O.userid')
    	             ->field('U.openuid,U.nickname,U.userid,U.uavatar,O.content,O.create_time,O.id,O.octoberid')
    	             ->where(array('octoberid'=>0))
    	             ->limit($page->firstRow . ',' . $page->listRows)
    	             ->order('O.create_time DESC')
    	             ->select();
    	
        foreach ($octoberOnt as $key => $value) {
        	$oid[] = $value['id'];
        }
        $octoberid = implode(",",$oid);
        $wheres['O.octoberid'] = array('IN',$octoberid);
        $octoberTwo = M('October') 
                     ->alias('O')
                     ->join('__USER__ U ON U.userid = O.userid')
                     ->field('U.openuid,U.nickname,U.userid,U.uavatar,O.content,O.create_time,O.id,O.tousername,O.octoberid')
                     ->where($wheres)
                     ->order('O.create_time ASC')
                     ->select();

        if($octoberTwo){

            $ocdata = array_merge_recursive($octoberOnt,$octoberTwo);

        }else{

            $ocdata = $octoberOnt;
        }
       
        foreach ($ocdata as $k => $v) {
            if($v['octoberid'] ==0){

                $octoberData[$v['id']] = $v;
                $octoberData[$v['id']]['htp'] = substr($v['uavatar'],0,7);

            }else{
                $octoberData[$v['octoberid']]['sou'][$k] = $v;
                $octoberData[$v['octoberid']]['sou'][$k]['htp'] = substr($v['uavatar'],0,7);
            }
        }

        $this->assign('oworks',$oWorks);
        $this->assign('works',$works);
        $this->assign('number',$number);
        $this->assign('count',$count);
        $this->assign('octoberData',$octoberData);
        $this->assign('page',$page->show());
        $this->display();
    }
    public function addAjax(){
    	
    	$content = I('post.con');
    	$tousername = I('post.tousername');
    	$octoberid = intval(I('post.octoberid'));
        
    	if(empty($tousername)){
    		$tousername = '';
    	}
    	if(empty($octoberid)){
    		$octoberid = 0;
    	}

    	$userid  = service('Passport')->userid;
    	if($userid){
    		$data['userid'] = $userid;
    		$data['tousername'] = $tousername;
    		$data['octoberid'] = $octoberid;
    		$data['content'] = strip_tags($content);
    		$data['create_time'] = time();
    		M('October')->add($data);

    	}else{
    		//没有登入的用户
    		echo 'NO';
    	}
    }

    public function deleteAjax(){
    	$id = I('post.id');
    	$data = M('October')->delete($id);
    	if(!$data){
    		echo "NO";
    	}
    }

}